package com.melichercik.app;

import com.example.bcapp.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import orbotix.robot.base.Robot;
import orbotix.robot.base.SpheroConfigResponse;
import orbotix.sphero.ConnectionListener;
import orbotix.sphero.Sphero;
import orbotix.view.connection.SpheroConnectionView;

/** Activity for controlling the Sphero with five control buttons. */
public class ConnectActivity extends Activity {
	
	GlobalState gs;
	
    private Sphero mRobot;
    
    private Activity activity = this;
    
    private int heading = 0;

    /** The Sphero Connection View */
    private SpheroConnectionView mSpheroConnectionView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	gs = (GlobalState) getApplication();
    	
    	mRobot = gs.robot;
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect);
        
        Button btnOff = (Button) this.findViewById(R.id.btn_sleep);
        Button btnLeft = (Button) this.findViewById(R.id.btn_left);
        Button btnRight = (Button) this.findViewById(R.id.btn_right);
        
        btnOff.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mRobot != null) {
					mRobot.sleep(0);
					gs.robot = null;
					mSpheroConnectionView.setVisibility(mSpheroConnectionView.VISIBLE);
		        }			
			}
		});
        
        btnLeft.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mRobot != null) {
	        		heading += 10;
		        	if (heading == 350 || heading > 359) {
		        		heading = 0;
		        	}
	            	mRobot.rotate(heading);
	            }		
			}
		});
        
        btnRight.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mRobot != null) {
	        		heading -= 10;
		        	if (heading < 0) {
		        		heading = 350;
		        	}
	            	mRobot.rotate(heading);
	            }		
			}
		});
        
        mSpheroConnectionView = (SpheroConnectionView) this.findViewById(R.id.sphero_connection_view);
        mSpheroConnectionView.addConnectionListener(new ConnectionListener() {

            @Override
            public void onConnected(Robot robot) {
                //SpheroConnectionView is made invisible on connect by default
                mRobot = (Sphero) robot;
                
                mRobot.rotate(heading);
                mRobot.startCalibration();
                
                gs.robot = mRobot;
                
//                Intent intent = new Intent(activity, MainActivity.class);
//        	    startActivity(intent);
            }

            @Override
            public void onConnectionFailed(Robot sphero) {
                // let the SpheroConnectionView handle or hide it and do something here...
            	Toast.makeText(activity, "Connection failure. Try it again.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDisconnected(Robot sphero) {
                mSpheroConnectionView.startDiscovery();
            }
        });
        
        if (mRobot != null) {
        	mSpheroConnectionView.setVisibility(mSpheroConnectionView.INVISIBLE);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        
        if (mRobot != null) {
        	mRobot.setBackLEDBrightness(0);
        	mRobot.stopCalibration(true);
        }
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.action_to_main:
	        	Intent intent = new Intent(activity, MainActivity.class);
        	    startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.sphero, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
}
